/* Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
 
Copyright (C) 2010 by Tomasz Pluskiewicz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */

using System;
using System.Web;
using Castle.Windsor;
using Yama.MVC.Core;

namespace Yama.MVC.Container
{
    ///<summary>
    /// Base class, which gives access to the Windsor container and exposes
    /// the <see cref="HttpApplication"/> lifecycle for easier overriding
    ///</summary>
    public abstract class WindsorHttpApplication : CoreHttpApplication, IContainerAccessor
    {
        private static IWindsorContainer _windsorContainer;

        #region Implementation of IContainerAccessor

        /// <summary>
        /// Provides a globally available access to the <see cref="IWindsorContainer" /> instance.
        /// </summary>
        public IWindsorContainer Container
        {
            get { return WindsorContainer; }
        }

        /// <summary>
        /// Provides a globally available access to the <see cref="IWindsorContainer" /> instance.
        /// </summary>
        public static IWindsorContainer WindsorContainer
        {
            get { return _windsorContainer; }
        }

        /// <summary>
        /// Runs WindsorContainer.Resolve(typeof(T))
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T Resolve<T>()
        {
            return (T)WindsorContainer.Resolve(typeof(T));
        }

        #endregion

        #region Virtual members

        ///<summary>
        /// Any code, which runs inside <see cref="System.Web.HttpApplication"/>#Application_Start.
        /// <see cref="WindsorHttpApplication"/> adds Windsor initalization. Do not forget a call to base.ApplicationStart(sender, e);
        ///</summary>
        public override void ApplicationStart(object sender, EventArgs e)
        {
            _windsorContainer = new Container(WindsorSectionName);
            WindsorSetup((Container) _windsorContainer);

            base.ApplicationStart(sender, e);
        }

        ///<summary>
        /// Name of the Castle Windsor section in web.config. Defaults to "windsor"
        ///</summary>
        public virtual string WindsorSectionName { get { return "windsor"; } }

        ///<summary>
        /// Implement to set up Castle Windsor Container
        ///</summary>
        public virtual void WindsorSetup(Container container) { }

        #endregion
    }
}